home *** CD-ROM | disk | FTP | other *** search
-
- #include <Types.h>
- //#include <SysEqu.h>
- #include <Resources.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <Menus.h>
- #include <Controls.h>
- #include <Dialogs.h>
- #include <Memory.h>
- #include <Files.h>
- #include <Devices.h>
- #include <Serial.h>
- #include <Timer.h>
- #include <OSUtils.h>
- #include <Processes.h>
- #include <Gestalt.h>
- #include <Errors.h>
- #include <string.h>
- #include <Speech.h>
-
-
- #define rMonitor 1000
- #define kSendButton 1
- #define kStopButton 2
- #define kQuitButton 3
- #define kMsgBox 4
- #define kSendSpinner 5
- #define kRcvSpinner 7
- #define kHoldFlag 9
- #define kBreakButton 10
-
- #define rSerDataRsrc 128
- #define rSpinnerIcon 1000
- #define rHeldIcon 128
- #define rNotHeldIcon 129
-
- #define rPortOpenALRT 256
- #define kReset 2
-
- #define kCtlEnable 0
- #define kCtlDisable 255
- #define kSerBufSize 16384
- #define kSerRdSize 64
- #define kSerConfig baud9600 + noParity + data8 + stop10
- #define kBreakLength 666 // serial break length in milliseconds
-
- #define drvrName 0x12 // offset to driver name in 'DRVR' std. header
-
- #define dOpened 5
- #define dRAMBased 6
- #define drvrActive 7 // Device Manager DCtlFlag bits
-
- #define kSerStatus 8
- #define kSerClrBrk 11 // Serial Driver csCodes
- #define kSerSetBrk 12
- #define kSerHShakeDTR 14
- #define breakR0 128 // mask for break bit in SCC RR0 -- See TN #56
- #define breakErr 8 // mask for break bit in cumErrs -- System 7.0
-
- #define kDVDName "\pApple DVD Player"
- #define kCDName "\pAppleCD Audio Player"
-
- /*
- typedef struct {
- IOParam fIOParam;
- long appA5;
- } ExtIOParam;
-
- typedef struct {
- TMTask fTMTask;
- long appA5;
- } ExtTMTask;
-
- typedef struct {
- unsigned char readR0;
- unsigned char deltaBits;
- short drvrPosting;
- } SERDEventMessage;
- */
-
-
- //Boolean gHeldOff,
- // gAllDone = false,
- // gShouldSend = false,
- // gReload = false,
- // gKillBreak = false,
- // gBreakReceived = false;
-
- Ptr gpSerBuf, *ghSerBuf, // serial receive buffer
- gpOutputData, *ghOutputData, // send buffer
- gpBitBucket, *ghBitBucket; // receive processing buffer
-
- long gOutputDataSize;
- IOParam *gpSendPB, **ghSendPB; // for PBWrite calls to send data
- short gSysVersion,
- gOutRefNum, gInRefNum,
- // gSendCount = 0,
- gBitBucketCount = 0;
- //char *panicString = "Help! I'm stuffed! And here's a bunch of characters to prove it!\n\r";
- TMTask gPostEventTask;
-
-
-
- short Initialize (void);
- void CleanUp (void);
- Boolean OpenSERD (void);
- void CloseSERD (void);
- void DoIOStuff (void);
- OSErr SendData (void);
- void CheckSerData (long reqBytes);
- void CheckSerStatus (void);
- pascal Boolean NullGrabber (DialogPtr, EventRecord *evt, short *itemHit);
- pascal void SendCompRout (void);
- pascal void FlagBreakTimeout (void);
- void ProcessBitBucket(void);
- void PrimeTMTask(void);
- void ProcessString(unsigned char* s);
-
- //#pragma parameter PostEventProc(__A0)
- pascal void PostEventProc (void);
-
- //OSErr AssertDrvrOpen (Str255 name, short *refNum);
-
- //ParmBlkPtr GetParmBlkPtr (void) = 0x2008; // MOVE.L A0,D0
- //TMTaskPtr GetTMTaskPtr (void) = 0x2009; // MOVE.L A1,D0
-
- //#pragma parameter __D0 PBControlImmed(__A0)
- //pascal OSErr PBControlImmed(ParmBlkPtr paramBlock) = 0xA204; // _Control ,IMMED
-
- //#pragma parameter __D0 PBStatusImmed(__A0)
- //pascal OSErr PBStatusImmed(ParmBlkPtr paramBlock) = 0xA205; // _Status ,IMMED
-
-
-
- void main (void)
- {
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- InitDialogs(NULL);
- InitCursor();
-
- // if (Initialize() == noErr) {
-
- if (OpenSERD()) { // open the Serial Driver
- DoIOStuff();
- CloseSERD(); // close the Serial Driver
- }
- CleanUp();
- // }
-
- }
-
- //#define kInputDriver "\p.AIn"
- //#define kOutputDriver "\p.AOut"
- #define kInputDriver "\p.IrIn"
- #define kOutputDriver "\p.IrOut"
-
-
- Boolean OpenSERD (void)
- {
- OSErr openOutErr, openInErr;
- OSErr setBufErr, setCfgErr, setHskErr;
- SerShk hskFlags;
- // long finalTicks;
- Boolean takeOverPort = true;
- // Boolean openAOut, openAIn;
-
- /* openAOut = AssertDrvrOpen(kOutputDriver, &gOutRefNum) == noErr;
- openAIn = AssertDrvrOpen(kInputDriver, &gInRefNum) == noErr;
- if (openAOut || openAIn) {
- if (takeOverPort = CautionAlert(rPortOpenALRT, nil) == kReset) {
- if (openAIn) {
- KillIO(gInRefNum);
- CloseDriver(gInRefNum);
- }
- if (openAOut) {
- KillIO(gOutRefNum);
- CloseDriver(gOutRefNum);
- }
- }
- }
-
- if (takeOverPort) {
- */
- openOutErr = OpenDriver(kOutputDriver, &gOutRefNum);
- openInErr = OpenDriver(kInputDriver, &gInRefNum);
-
- if (openOutErr == noErr && openInErr == noErr) {
-
- // It's always good to first set a non-default input buffer, if desired.
- // There is no output buffering, so specify only the input driver.
-
- setBufErr = SerSetBuf(gInRefNum, gpSerBuf, kSerBufSize);
-
- hskFlags.fXOn = false;
- hskFlags.fCTS = true;
- hskFlags.xOn = 0x11;
- hskFlags.xOff = 0x13;
- hskFlags.errs = 0;
-
- if (gSysVersion >= 0x0700) {
- hskFlags.evts = 0; // I can use new means of break detection.
- }
- else {
- hskFlags.evts = breakEvent; // I need the driver to post break events.
- }
-
- hskFlags.fInX = false;
- hskFlags.fDTR = true;
-
- // SerHShake() does not support full DTR/CTS hardware handshaking. You
- // accomplish the same thing and more with a Control call and csCode 14.
- // You only need to specify hskFlags once, to the output driver.
-
- setHskErr = Control(gOutRefNum, kSerHShakeDTR, (Ptr) &hskFlags);
-
- // Now reset both input and output drivers with the same configuration.
- // Only a single call to the output driver is necessary to do this.
- // Differing concurrent input/output baud rates are not supported.
-
- setCfgErr = SerReset(gOutRefNum, kSerConfig);
-
- }
- else DebugStr("\pCan't open driver");
- // }
-
- return takeOverPort;
- }
-
-
-
- void CloseSERD (void)
- {
- OSErr killErr, closeOutErr, closeInErr;
-
- killErr = KillIO(gInRefNum);
- closeInErr = CloseDriver(gInRefNum);
-
- killErr = KillIO(gOutRefNum);
- closeOutErr = CloseDriver(gOutRefNum);
-
- }
-
-
-
-
- void CheckSerData (long /*reqBytes*/)
- {
- OSErr checkBufErr, serRdErr;
- long charCount;
-
- // long finalTicks;
- // register long overrun;
-
- checkBufErr = SerGetBuf(gInRefNum, &charCount);
- if (checkBufErr == noErr) {
-
- // The general strategy here is this: if number of available characters
- // meets a certain minimum threshold, then I read in everything in the
- // buffer. If I get delayed, I'll catch up quickly.
-
- // if (charCount != 0 && charCount >= reqBytes) {
- if (charCount > 0) {
-
- serRdErr = FSRead(gInRefNum, &charCount, &gpBitBucket[gBitBucketCount]);
- if (serRdErr == noErr) {
- gBitBucketCount += charCount;
- gpBitBucket[gBitBucketCount] = 0xFF; // append an 'unfinished string' terminator
- // DebugStr("\pgotsome!");
- }
- }
- }
- }
-
-
-
- void DoIOStuff (void)
- {
- // DialogPtr serMonitor;
- // OSErr primeErr;
- // short itemHit, itemType;
- // ControlHandle sendItem, stopItem, breakItem;
- // Handle spinner, flag, item;
- // Rect box;
- // CntrlParam breakPB;
-
- // serMonitor = GetNewDialog(rMonitor, NULL, NULL);
- // SetPort((GrafPtr) serMonitor);
-
- // send some data
- SendData();
-
- // PrimeTMTask();
-
- while(!Button())
- {
- EventRecord theEvent;
- CheckSerData(4);
- WaitNextEvent(everyEvent, &theEvent, 10, 0);
-
- ProcessBitBucket();
-
- /* if(gRcvCount >= 4)
- {
- gpBitBucket[5] = 0;
- c2pstr(gpBitBucket);
- DebugStr((unsigned char*)gpBitBucket);
- }
- */
- }
-
- }
-
-
- void ProcessBitBucket(void)
- {
- char* s = gpBitBucket;
- short i = 0;
- Boolean foundString = false;
-
- while(foundString == false && i < gBitBucketCount)
- {
- if(s[i] == '\r')
- {
- foundString = true;
- s[i] = 0; // null terminate the string
- }
- if(s[i] == 0xFF)
- {
- foundString = false;
- break;
- }
-
- i++;
- }
-
- if(foundString == true)
- {
- c2pstr(s);
- SpeakString((unsigned char*)s);
-
- ProcessString((unsigned char*)s);
-
- gBitBucketCount -= i;
- BlockMove(&s[i], &s[0], gBitBucketCount);
- }
-
- return;
- }
-
- void ProcessString(unsigned char* s)
- {
- if(EqualString(s, "\pPlay", false, false))
- {
- PostEvent(keyDown, ' ');
- }
- else if(EqualString(s, "\pPause", false, false))
- {
- PostEvent(keyDown, ' ');
- }
- else if(EqualString(s, "\pStop", false, false))
- {
-
- }
- else if(EqualString(s, "\pFast Forward", false, false))
- {
-
- }
- else if(EqualString(s, "\pRewind", false, false))
- {
-
- }
-
- return;
- }
-
- OSErr SendData (void)
- //short SendData (void)
- {
- gpSendPB->ioCompletion = 0;//(ProcPtr)0;// SendCompRout;
- gpSendPB->ioRefNum = gOutRefNum;
- gpSendPB->ioBuffer = gpOutputData;
- gpSendPB->ioReqCount = gOutputDataSize;
- // ((ExtIOParam *) gpSendPB)->appA5 = SetCurrentA5(); // completion routine needs A5
- // gReload = true;
-
- // return PBWriteAsync((ParmBlkPtr) gpSendPB); // asynchronous self-sustaining sends
- return PBWriteSync((ParmBlkPtr) gpSendPB); // asynchronous self-sustaining sends
-
- }
-
-
-
-
-
-
- void CleanUp (void)
- {
-
- DisposeHandle((Handle) ghSendPB);
- DisposeHandle(ghSerBuf);
- DisposeHandle(ghBitBucket);
- }
-
-
-
- #pragma mark • Commented out
-
-
- /*
- while (!gAllDone) {
-
- CheckSerStatus();
-
-
- if (gShouldSend && gReload) {
- gSendCount++; // increment a counter for the output spinner
- gReload = !gReload;
- }
-
- // The break timer simply sets a global flag which I use to indicate when
- // to clear a break condition. Again, I use an immediate Control call, but
- // primarily for consistency, and also to show off.
-
- if (gKillBreak) {
- breakPB.ioCRefNum = gOutRefNum;
- breakPB.csCode = kSerClrBrk;
- PBControlImmed((ParmBlkPtr) &breakPB); // SerClrBrk(), but IMMED
- gKillBreak = !gKillBreak;
- GetDialogItem(serMonitor, kBreakButton, &itemType, &(Handle) breakItem, &box);
- HiliteControl(breakItem, kCtlEnable);
- }
-
- // If another area of the program detects a break, I flag the occurrence here.
-
- if (gBreakReceived) {
- SysBeep(1);
- SysBeep(1);
- gBreakReceived = !gBreakReceived;
- }
-
-
- // In lieu of an event loop, I just use a modal dialog with a relatively
- // simple (but unusual) filterProc. This is not a good example of how to
- // write an app. Modal dialogs are evil and to be avoided if possible.
- // Nonetheless, the filterProc is an interesting example unto itself....
-
- ModalDialog(NullGrabber, &itemHit);
- switch (itemHit) {
-
- case kStopButton:
- if (gShouldSend) {
- gShouldSend = !gShouldSend;
- }
- break;
-
- case kSendButton:
- if (!gShouldSend) {
- gShouldSend = !gShouldSend;
- primeErr = SendData();
- }
- break;
-
- case kBreakButton:
-
- // In another possible Mac IIfx IOP bug, SerSetBrk() called synchronously
- // appears to hang the machine if an async write is pending. Since that is
- // often the case (at least in this application) I work around the problem
- // by making the Control call immediate--this prevents the hang, but also
- // raises another interesting issue about when break is actually asserted.
-
- breakPB.ioCRefNum = gOutRefNum;
- breakPB.csCode = kSerSetBrk;
- PBControlImmed((ParmBlkPtr) &breakPB); // SerSetBrk(), but IMMED
-
- // With break asserted, I prime a Time Manager task to flag the end of
- // the break, and disable the Break button so that it cannot be selected
- // again until break is negated.
-
- PrimeTime((QElemPtr) &gUnBreakTask, kBreakLength);
- GetDialogItem(serMonitor, kBreakButton, &itemType, &(Handle) breakItem, &box);
- HiliteControl(breakItem, kCtlDisable);
- break;
-
- case kQuitButton:
- gAllDone = true;
- break;
-
- }
- }
- */
-
- // DisposeDialog(serMonitor);
-
-
- /*
- pascal void FlagBreakTimeout (void)
- {
- long oldA5;
-
- oldA5 = SetA5(((ExtTMTask *) GetTMTaskPtr())->appA5); // retrieve A5
-
- gKillBreak = true;
-
- SetA5(oldA5);
-
- }
- */
-
-
- /*
- pascal Boolean NullGrabber (DialogPtr dPtr, EventRecord *evt, short *itemHit)
- {
- EventRecord driverEvent;
-
- // Without this filterProc, none of the animation works:
-
- // In order to keep things rolling along even when there are no events
- // such as mouse clicks or keystrokes, I have to return true in response
- // to null events. This is unusual, but otherwise ModalDialog() "handles"
- // null events by eating them and waiting for something better. This is
- // bad if I need to turn the spinner or clear a break condition.
-
- // Also, I check for driver events here in order to detect breaks.
-
- if (GetNextEvent(driverMask, &driverEvent)) {
- if ((*(SERDEventMessage *) &driverEvent.message).drvrPosting == gInRefNum) {
- gBreakReceived = ((*(SERDEventMessage *) &driverEvent.message).readR0 & breakR0) != 0;
- }
- }
-
- if (evt->what == nullEvent) {
- *itemHit = 0;
- return true;
- }
-
- else {
- return false;
- }
-
- }*/
-
- /*
-
- pascal void SendCompRout (void)
- {
- long oldA5;
-
- oldA5 = SetA5(((ExtIOParam *) GetParmBlkPtr())->appA5); // retrieve A5
-
- if (gShouldSend && !gAllDone) {
- gReload = true;
- gpSendPB->ioCompletion = (ProcPtr) SendCompRout;
- PBWriteAsync((ParmBlkPtr) gpSendPB); // this is the self-sustaining part
- }
-
- SetA5(oldA5);
-
- }
- */
-
- /*
- OSErr AssertDrvrOpen (Str255 name, short *refNum)
- {
- DCtlHandle *pUTEntry;
- Ptr pDrvr;
- OSErr result = notOpenErr; // assume not open
- short unitNo;
- char *aDrvrName;
-
- // The point here is to determine whether a driver is open, given its name.
- // This allows one to check a driver to see if it's open without hard coding
- // its reference number. (Normally, the way to get the refNum is to open
- // the driver--but that defeats the whole purpose!)
- // This is an extension of the code discussed in Tech Note #71.
-
- *refNum = 0;
- pUTEntry = *(DCtlHandle **) UTableBase;
- for (unitNo = 0; unitNo < *(short *) UnitNtryCnt; unitNo++, pUTEntry++) {
- if (*pUTEntry != nil && **pUTEntry != nil) {
- if (((***pUTEntry).dCtlFlags & 1 << dRAMBased) != 0)
- pDrvr = *(Handle) (***pUTEntry).dCtlDriver;
- else
- pDrvr = (***pUTEntry).dCtlDriver;
-
- if (pDrvr != nil) {
- aDrvrName = pDrvr + drvrName;
- if (memcmp(aDrvrName, name, 1 + name[0]) == 0) {
- // We found the one we're after.
- *refNum = ~unitNo;
- if (((***pUTEntry).dCtlFlags & 1 << dOpened) != 0)
- result = noErr;
- break;
- }
- }
- }
- }
-
- return result;
- }
-
- */
-
-
- /*
- void CheckSerStatus (void)
- {
- OSErr checkStatErr, panicErr;
- IOParam altSendPB, *pAltSendPB = &altSendPB;
- CntrlParam statPB;
- SerStaRec serStat;
-
- statPB.ioCRefNum = gInRefNum;
- statPB.csCode = kSerStatus;
- checkStatErr = PBStatusImmed((ParmBlkPtr) &statPB);
- serStat = *(SerStaRec *) &statPB.csParam;
-
- // I check to see if the remote system has told me to stop sending.
-
- gHeldOff = (Boolean) serStat.ctsHold;
-
- // Check for errors.
-
- if (serStat.cumErrs & swOverrunErr)
- DebugStr("\pSoftware Overrun Error");
- else if (serStat.cumErrs & parityErr)
- DebugStr("\pParity Error");
- else if (serStat.cumErrs & hwOverrunErr)
- DebugStr("\pHardware Overrun Error");
- else if (serStat.cumErrs & framingErr)
- DebugStr("\pFraming Error");
-
- // If I have System 7.0 or better, I can check directly to see if
- // I've received a break. Usually I don't check for a feature this
- // way, but in this case I have no alternative.
-
- if (gSysVersion >= 0x0700) {
- gBreakReceived = (serStat.cumErrs & breakErr) != 0;
- }
-
- // All I do here is send a small "panic" packet of characters back
- // to the remote system when it fills _my_ buffer. I don't actually
- // know the exact state of my buffer, but I can see if I've told
- // the remote system to shut up, indicating that I'm mostly full.
-
- if (checkStatErr == noErr) {
- if (( (unsigned char) serStat.xOffSent & dtrNegated) != 0) {
- pAltSendPB->ioCompletion = NULL;
- pAltSendPB->ioRefNum = gOutRefNum;
- pAltSendPB->ioBuffer = panicString;
- pAltSendPB->ioReqCount = strlen(panicString);
- PBWriteAsync((ParmBlkPtr) pAltSendPB);
-
- // The program may hang here if the user quits the
- // remote application first--that could hold off our
- // serial output, leaving a pending asynchronous I/O
- // request and keeping us in an infinite loop.
-
- while (pAltSendPB->ioResult > 0) {} // I'll fix it later.
- // The reason I do this instead of just calling it synchronously
- // is so that if I do hang, I'll hang in my code for an obvious
- // reason instead of hanging up in the Device Manager.
- panicErr = pAltSendPB->ioResult;
- }
- }
-
-
- }
- */